//
//  NetworkErrorView.swift
//  ShareVipForBusiness
//
//  Created by OzIntel on 2017/11/7.
//  Copyright © 2017年 zev. All rights reserved.
//

import UIKit
import SnapKit
typealias refreshClosure = () -> Void
class NetworkErrorView: UIView {
    var refreshClosure : refreshClosure?
    lazy var errorImageView: UIImageView = {
        // imageView
        let errorImageView = UIImageView.init()
        errorImageView.image = UIImage.init(named: "netword")
        return errorImageView
    }()
    
    lazy var infoLabel: UILabel = {
        // label
        let infoLabel = UILabel.init()
        infoLabel.text = "加载失败"
        infoLabel.textAlignment = .center
        infoLabel.textColor = UIColor.withHex(hexString: "a4a4a5")
        infoLabel.font = UIFont.systemFont(ofSize: 18)
        return infoLabel
    }()
    
    lazy var refreshButton : UIButton = {
        let button = UIButton.init(type: .custom)
        button.setTitle("刷新重试", for: .normal)
        button.setBackgroundImage(UIImage.init(color: blueColor), for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        button.setTitleColor(UIColor.white, for: .normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 25
        button.addTarget(self, action: #selector(refresh), for: .touchUpInside)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension NetworkErrorView {
    
    func setupUI() {
        self.tag = 997
        self.backgroundColor = UIColor.white
        self.addSubview(infoLabel)
        self.addSubview(errorImageView)
        self.addSubview(refreshButton)
    }
    
    override func layoutSubviews() {
        infoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(20)
            make.right.equalTo(self).offset(-20)
            make.top.equalTo(errorImageView.snp.bottom).offset(30)
        }
        
        errorImageView.snp.makeConstraints { (make) in
            if self.frame.height <= 568{
                make.top.equalTo(self).offset(100)
            }else{
                make.top.equalTo(self).offset(176)
            }
            make.centerX.equalTo(self)
            make.width.equalTo(120)
            make.height.equalTo(120)
        }
        
        refreshButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(infoLabel.snp.bottom).offset(18)
            make.width.equalTo(120)
            make.height.equalTo(50)
        }
    }
    
    @objc func refresh(sender : UIButton){
        if refreshClosure != nil {
            self.removeFromSuperview()
            refreshClosure!()
        }
    }
    
}
